import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.bargain.activity.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.bargain.activity.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'startTime',
      label: $t('views.mall.promotion.bargain.activity.startTime'),
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        placeholder: $t('views.mall.promotion.bargain.activity.startTimePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'endTime',
      label: $t('views.mall.promotion.bargain.activity.endTime'),
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        placeholder: $t('views.mall.promotion.bargain.activity.endTimePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'bargainFirstPrice',
      label: $t('views.mall.promotion.bargain.activity.bargainFirstPrice'),
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
        step: 0.01,
        placeholder: $t('views.mall.promotion.bargain.activity.bargainFirstPricePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'bargainMinPrice',
      label: $t('views.mall.promotion.bargain.activity.bargainMinPrice'),
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
        step: 0.01,
        placeholder: $t('views.mall.promotion.bargain.activity.bargainMinPricePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'stock',
      label: $t('views.mall.promotion.bargain.activity.stock'),
      component: 'InputNumber',
      componentProps: {
        min: 1,
        placeholder: $t('views.mall.promotion.bargain.activity.stockPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'helpMaxCount',
      label: $t('views.mall.promotion.bargain.activity.helpMaxCount'),
      component: 'InputNumber',
      componentProps: {
        min: 1,
        placeholder: $t('views.mall.promotion.bargain.activity.helpMaxCountPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'bargainCount',
      label: $t('views.mall.promotion.bargain.activity.bargainCount'),
      component: 'InputNumber',
      componentProps: {
        min: 1,
        placeholder: $t('views.mall.promotion.bargain.activity.bargainCountPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'totalLimitCount',
      label: $t('views.mall.promotion.bargain.activity.totalLimitCount'),
      component: 'InputNumber',
      componentProps: {
        min: 1,
        placeholder: $t('views.mall.promotion.bargain.activity.totalLimitCountPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'randomMinPrice',
      label: $t('views.mall.promotion.bargain.activity.randomMinPrice'),
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
        step: 0.01,
        placeholder: $t('views.mall.promotion.bargain.activity.randomMinPricePlaceholder'),
      },
    },
    {
      fieldName: 'randomMaxPrice',
      label: $t('views.mall.promotion.bargain.activity.randomMaxPrice'),
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
        step: 0.01,
        placeholder: $t('views.mall.promotion.bargain.activity.randomMaxPricePlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.mall.promotion.bargain.activity.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.mall.promotion.bargain.activity.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 80,
    },
    {
      field: 'name',
      title: $t('views.mall.promotion.bargain.activity.name'),
      minWidth: 140,
    },
    {
      field: 'activityTime',
      title: $t('views.mall.promotion.bargain.activity.activityTime'),
      minWidth: 210,
      formatter: ({ row }) => {
        if (!row.startTime || !row.endTime) return '';
        return `${formatDate(row.startTime, 'YYYY-MM-DD')} ~ ${formatDate(row.endTime, 'YYYY-MM-DD')}`;
      },
    },
    {
      field: 'picUrl',
      title: $t('views.mall.promotion.bargain.activity.picUrl'),
      minWidth: 80,
      cellRender: {
        name: 'CellImage',
        props: {
          height: 40,
          width: 40,
        },
      },
    },
    {
      field: 'spuName',
      title: $t('views.mall.promotion.bargain.activity.spuName'),
      minWidth: 300,
    },
    {
      field: 'bargainFirstPrice',
      title: $t('views.mall.promotion.bargain.activity.bargainFirstPrice'),
      minWidth: 130,
      formatter: 'formatAmount2',
    },
    {
      field: 'bargainMinPrice',
      title: $t('views.mall.promotion.bargain.activity.bargainMinPrice'),
      minWidth: 130,
      formatter: 'formatAmount2',
    },
    {
      field: 'recordUserCount',
      title: $t('views.mall.promotion.bargain.activity.recordUserCount'),
      minWidth: 130,
    },
    {
      field: 'recordSuccessUserCount',
      title: $t('views.mall.promotion.bargain.activity.recordSuccessUserCount'),
      minWidth: 130,
    },
    {
      field: 'helpUserCount',
      title: $t('views.mall.promotion.bargain.activity.helpUserCount'),
      minWidth: 130,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'stock',
      title: $t('views.mall.promotion.bargain.activity.stock'),
      minWidth: 80,
    },
    {
      field: 'totalStock',
      title: $t('views.mall.promotion.bargain.activity.totalStock'),
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 150,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
